package ar.uba.fi.tonyvaliente.tree;

import java.io.File;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ar.uba.fi.tonyvaliente.persistors.StringPersistor;

public class BPlusTreeTest {

	public static Log log = LogFactory.getLog(BPlusTreeTest.class);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		BPlusTree<String, String> tree = null;
		try {
//			BPlusTree<String, String> tree = new MemoryBPlusTree<String, String>(2);
			

			new File("/home/santiago/bplustree").delete();
			
			tree = new DiskBPlusTree<String, String>(5, "/home/santiago/bplustree", new StringPersistor(), new StringPersistor());

			String value;

			tree.add("aun", "Kaun");
			value = tree.get("aun");
			log.info("aun: " + value);
			tree.add("d", "d");
			value = tree.get("d");
			log.info("d: " + value);
			tree.add("a", "Ka");
			value = tree.get("a");
			log.info("a: " + value);
			tree.add("dardo", "dardo");
			value = tree.get("dardo");
			log.info("dardo: " + value);
			tree.add("anda", "Kanda");
			value = tree.get("anda");
			log.info("anda: " + value);
			tree.add("dia", "dia");
			value = tree.get("dia");
			log.info("dia: " + value);
			tree.add("denle", "denle");
			value = tree.get("denle");
			log.info("denle: " + value);
			tree.add("algo", "Kalgo");
			value = tree.get("algo");
			log.info("algo: " + value);

			tree.add("b", "Kb");
			value = tree.get("b");
			log.info("b: " + value);
			tree.add("cosa", "cosa");
			value = tree.get("cosa");
			log.info("cosa: " + value);
			tree.add("barco", "Kbarco");
			value = tree.get("barco");
			log.info("barco: " + value);
			tree.add("casa", "casa");
			value = tree.get("casa");
			log.info("casa: " + value);
			tree.add("bote", "Kbote");
			value = tree.get("bote");
			log.info("bote: " + value);
			tree.add("chau", "chau");
			value = tree.get("chau");
			log.info("chau: " + value);
			tree.add("bueno", "Kbueno");
			value = tree.get("bueno");
			log.info("bueno: " + value);
			tree.add("c", "c");
			value = tree.get("c");
			log.info("c: " + value);
			value = tree.get("malo");
			log.info("malo: " + value);
			
			log.info("---------------------------------");
			log.info("FIN DE LOS INSERTS - CORROBORANDO");
			log.info("---------------------------------");
			
			value = tree.get("aun");
			log.info("aun: " + value);
			value = tree.get("d");
			log.info("d: " + value);
			value = tree.get("a");
			log.info("a: " + value);
			value = tree.get("dardo");
			log.info("dardo: " + value);
			value = tree.get("anda");
			log.info("anda: " + value);
			value = tree.get("dia");
			log.info("dia: " + value);
			value = tree.get("denle");
			log.info("denle: " + value);
			value = tree.get("algo");
			log.info("algo: " + value);

			value = tree.get("b");
			log.info("b: " + value);
			value = tree.get("cosa");
			log.info("cosa: " + value);
			value = tree.get("barco");
			log.info("barco: " + value);
			value = tree.get("casa");
			log.info("casa: " + value);
			value = tree.get("bote");
			log.info("bote: " + value);
			value = tree.get("chau");
			log.info("chau: " + value);
			value = tree.get("bueno");
			log.info("bueno: " + value);
			value = tree.get("c");
			log.info("c: " + value);
			value = tree.get("malo");
			log.info("malo: " + value);
			
			/*
			Reference<String, String> primero = tree.getFirst();
			
			while(primero != null){
				try{
					System.out.println(primero.getNode());
				}catch(NullPointerException npe){
					log.error(npe.getMessage());
				}
				primero = ((LeafBPlusTreeNode<String, String>)primero.getNode()).getNext();
			}
			*/
			
			log.info("----------------------------------------");
			log.info("Iterando resultados: pasada 1");
			Iterator<String> it = tree.iterator();
			
			while(it.hasNext()){
				log.info("clave: " + it.next());
			}
			
			tree.close();
			
			log.info("---------------------------------");
			log.info("Reabriendo el árbol");
			log.info("---------------------------------");
			
			tree = new DiskBPlusTree<String, String>("/home/santiago/bplustree", 
					false, new StringPersistor(), new StringPersistor());
			
			value = tree.get("aun");
			log.info("aun: " + value);
			value = tree.get("d");
			log.info("d: " + value);
			value = tree.get("a");
			log.info("a: " + value);
			value = tree.get("dardo");
			log.info("dardo: " + value);
			value = tree.get("anda");
			log.info("anda: " + value);
			value = tree.get("dia");
			log.info("dia: " + value);
			value = tree.get("denle");
			log.info("denle: " + value);
			value = tree.get("algo");
			log.info("algo: " + value);

			value = tree.get("b");
			log.info("b: " + value);
			value = tree.get("cosa");
			log.info("cosa: " + value);
			value = tree.get("barco");
			log.info("barco: " + value);
			value = tree.get("casa");
			log.info("casa: " + value);
			value = tree.get("bote");
			log.info("bote: " + value);
			value = tree.get("chau");
			log.info("chau: " + value);
			value = tree.get("bueno");
			log.info("bueno: " + value);
			value = tree.get("c");
			log.info("c: " + value);
			value = tree.get("malo");
			log.info("malo: " + value);
			
			
			log.info("----------------------------------------");
			log.info("Iterando resultados: pasada 2");
			it = tree.iterator();
			
			while(it.hasNext()){
				log.info("clave: " + it.next());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			
			try {
				if(tree != null){
					tree.close();
				}
			} catch (BPlusTreeException e) {
				e.printStackTrace();
			}
		}
	}

}
